<template>
  <BaseTable
    ref="baseTableRef"
    :apiUrl="info?.dictId ? getDictList : null"
    :columns="columns"
    v-if="!loading"
    placeholder="请输入名称"
    height="calc(100vh - 200px)"
    :rowId="info?.dictId"
    @handleEditOrAdd="$refs.addOrEditRef.open({ dictType: info?.dictType })"
  >
    <!-- <template #required="{ record }">
      <ga-switch
        size="mini"
        v-model="record.required"
        @change="handleEnabledChange(record)"
      />
    </template> -->
    <template #action="{ record }">
      <ga-link @click="$refs.addOrEditRef.open(record)">编辑</ga-link>
      <ga-link status="danger" @click="handleDelete(record)">删除</ga-link>
    </template>
  </BaseTable>
  <addOrEdit ref="addOrEditRef" @fetchData="$refs.baseTableRef.fetchData()" />
</template>

<script setup>
import { ref, watch } from "vue";
import addOrEdit from "./addOrEdit.vue";
import { columns } from "./columns.js";
import { getDictList, deleteDict } from "@/api/dataDictionary";
import useLoading from "@/hooks/loading";
const { loading, setLoading } = useLoading(false);
import { warningModal } from "@/hooks/useWarningModal";
import { Message } from "@garco-design/web-vue";

const baseTableRef = ref(null);
// 删除方法
const handleDelete = (record) => {
  warningModal(`确定要删除-[${record?.dictLabel}]`, async () => {
    try {
      await deleteDict(record?.dictCode);
      Message.success("操作成功");
      baseTableRef.value.fetchData();
    } catch (err) {
      console.log(err);
    }
  });
};
const props = defineProps({
  info: {
    type: Object,
    default: () => {},
  },
});
watch(
  () => props.info,
  () => {
    setLoading(true);
    setTimeout(() => {
      setLoading(false);
    }, 0);
  }
);
</script>

<style lang="scss" scoped></style>
